<?
	include_once("cmsFiles.php");
	
	//extract the folderid..
	$folder = extractNumberWithKeyFromArray('folder', $_REQUEST, null);
	if (!is_null($folder)) {
		$cmsDocument->addConstraint('organisationalname',$folder,'=');
	}
	
	$upUrl = $_SERVER['PHP_SELF'] . '?formNo=1&formid=150';
	$returnURL = $_SERVER['PHP_SELF'] . "?fileNo=1&formid=147&sorts=clear&direction=clear&pageNumber=$pageNumber&folder=$folder";
	$cancelURL = $returnURL;	
?>
<TABLE class="cms_browsetext" cellSpacing="0" cellPadding="0" width="100%" border="0">
    <TR> 
      <TH align="left" width="35">&nbsp;</TH>
      <TH align="left" ><A href="/cms/html/index.php?sorts=title&direction=<?= ($direction == "DESC") ? "ASC" : "DESC" ?>">Title</A></TH>
      <TH width="130" align="left">Action</TH>
    </TR>

<?
	$sortColumns = array();
	if (is_string($sorts)) {
		if(!is_string($direction)) {
			$direction = "DESC";
		}
		$sortColumns = explode(",", $sorts);
		foreach($sortColumns as $sort){
			$cmsDocument->addSort($sort, $direction);	
		}	
	}

	$cmsDocument->resetIterator();
	
	if ($cmsDocument->totalDocuments() > 0) {
		

		while ($row = $cmsDocument->nextDocument()){
			$documentDetails = getFileDetails($row,  $db);
			$iconUrl = iconForMimeType($documentDetails->mimetype);
			
			$downloadUrl = "/html/blob.php?attach=true&documentCode=" . $row->documentcode ;
?>

                    <TR> 
                      <TD align="left" colSpan="3"><IMG height="7" src="/cms/images/greyline.gif" width="100%" /></TD>
                    </TR>
                    <TR> 
                      <TD class="cms_browsetext" width="35"><a href="/cms/html/index.php?fileNo=102&documentCode=<?= $row->documentcode ?>&folder=<?= $folder ?>"><img src="<?=$iconUrl?>" width="35" height="20" /></a></TD>
                      <TD class="cms_browsetext" ><a href="/cms/html/index.php?fileNo=102&documentCode=<?= $row->documentcode ?>&folder=<?= $folder ?>"><?= $row->title ?></a></TD>                      
                      <TD class="cms_browsetext" width="130">
<?

						if (viewFormAuth($authInfo, $formDescription, $row->documentcode, $db)){
?>
<a target="_blank" href="/html/blob.php?attach=false&documentCode=<?= $row->documentcode ?>">View</a>
<?
						}
                      	if(editFormAuth($authInfo, $formDescription, $row->documentcode, $db)) { 
?>
/                      	<a href="/cms/html/index.php?fileNo=102&documentCode=<?= $row->documentcode ?>&folder=<?= $folder ?>" >Details</a>
<? 
                      	}
						if(deleteFormAuth($authInfo, $formDescription, $row->documentcode, $db)) {
?>
 / <a href="/cms/html/index.php?fileNo=4&documentCode=<? print($row->documentcode); ?>&returnURLEncoded=<?= urlencode($returnURL) ?>&cancelURLEncoded=<?= urlencode($cancelURL) ?>" >Delete</a>
<? 
						}
?>
						</td>
                    </TR>
<?
		}
	} else {
		print <<< EOD
<TR> 
<TD align="left" colSpan="3"><IMG height="7" src="/cms/images/greyline.gif" width="100%" /></TD>
</TR>
<TR> 
<TD class="cms_browsetext" width="35"><img src="/cms/images/spacer.gif" height="20" /></TD>
<TD class="cms_browsetext" ><I>There are no files in this folder.</I></TD>
<TD class="cms_browsetext" width="130">&nbsp;</TD>
</TR>
EOD;
	}
?>


    <TR> 
      <TD align="left" colSpan="3"><IMG height="7" src="/cms/images/greyline.gif" width="100%"></TD>
    </TR>

    <TR> 
      <TD class="cms_browsetext" width="35"><a href="<?= $upUrl ?>"><img src="/cms/html/files/images/icon_folderup.gif" width="35" height="20" /></a></TD>

      <TD class="cms_browsetext" ><a href="<?= $upUrl ?>"><i>.. Back to parent folder.<i></a></TD>
      <TD class="cms_browsetext" width="130"></td>
    </TR>
     <TR> 
      <TD align="left" colSpan="3"><IMG height="7" src="/cms/images/greyline.gif" width="100%"></TD>
    </TR>   
</TABLE>


<?

//not sure about the returnurl.. need to have a method of restoring the stack correctly..
	$folderGid = null;
	if (!is_null($folder)) {
		$folderContent = $cmsDocument->openDocument($folder);
		if (!is_null($folderContent)) {
			$folderGid = $folderContent->gid;
		}
	}

//	displayGoAdd(
//		$formDescription, 
//		$authInfo,
//		$folderGid,
//		array(
//			'fileNo'=>2,
//			'formId'=> 147,
//			'organisationalname'=>$folder,
//			'returnURL' => urlencode( $returnURL )//,
//			//'cancelURLEncoded' => urlencode( $returnURL )// we use return instead of a different cancel as we want to come back to the same place
//		),
//		$db
//	);
	$addMode = CMS_ADD_QUICK_ADD_GO;
	$addUrlDict = array(
			'fileNo'=>2,
			'formId'=> 147,
			'organisationalname'=>$folder,
			'returnURL' =>  $returnURL ,//return url in this is encoded when the input is written out.
			'gid' => $folderGid
	);


?>